{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# User's Guide, Chapter 58: Understanding Sites and Contexts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Music21` contains a powerful context and hierarchy system that lets users find analytical information in a single place that actually requires looking at many pieces of information scattered throughout a score. Take for instance, a fragment of code that we use a lot such as:\n",
    "\n",
    "```\n",
    ">>> n.beat\n",
    "4\n",
    "```\n",
    "\n",
    "It's great when it works, but then there are times when it doesn't and it's just frustrating.  Avoiding those times is what this chapter is about.  And to do so, we'll need to start asking some \"how\" questions.  \n",
    "\n",
    "How does a note know what beat it is on? It might help to think about when we read a printed score, how do we know what beat a note is on?  We have to look at the note, then look up the score to find the most recent time signature, then find the note again and look at the measure it is in, count everything preceeding it in the measure, and then calculate the beat.  At least three different musical objects need to be consulted: the note itself, the surrounding measure, and the time signature that provides an interpretation of how note durations translate to beats.  `Music21` needs to do the same search, and it does all that just on a little call to the `.beat` property.\n",
    "\n",
    "(*In the early days of `music21`, I did not know about the convention that properties should be fast and easily computable so that the user does not even realize it is something more complex than an attribute lookup.  The property `.beat` is none of the above.  If I were starting over, it would be a method, `.beat()`, but it is too late to change now.*)\n",
    "\n",
    "\n",
    "To understand how this lookup works, we will need to understand better how `Sites` and `Contexts` work. Advanced users and beginners alike (and occasionally even the `music21` developers) are frequently confused by `music21`'s context and hierarchy system. When it works, it works great, it's just magic.  But, when it doesn't, it appears to be a random bug, and it is probably the most common type of bug mention on the `music21` GitHub tracker that gets \"not a bug\" as a response. Magic is fickle that way.\n",
    "\n",
    "Let's start by looking at a simple example.  We will create a measure and add a single E-flat to it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2.0} <music21.note.Note E->\n"
     ]
    }
   ],
   "source": [
    "m = stream.Measure(number=1)\n",
    "es = note.Note('E-4')\n",
    "m.insert(2, es)\n",
    "m.show('text')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At this point, there's obviously a connection made between the Measure and the Note."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.note.Note E->"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.stream.Measure 1 offset=0.0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.activeSite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.offset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.elementOffset(es)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But we need to know, what is the nature of this connection?  It has changed several times as `music21` has developed but has been stable since at least v.4, and it looks to stay that way.  The measure (or any `Stream`) contains an ordered list of the elements in it, and it also contains a mapping of each element in it to its offset.\n",
    "\n",
    "The element in a stream (such as a Note) does not however, have a direct list of what stream or streams it is in.  Instead elements have a property `.sites` that is itself a rather complex object called :class:`~music21.site.Sites` that keeps track of this information for it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.sites.Sites at 0x10c806488>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.sites"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Working with Sites"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Sites` object keeps track of how many and which streams an element has been placed in:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.sites.getSiteCount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = stream.Voice()\n",
    "v.append(es)\n",
    "es.sites.getSiteCount()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we need to figure out a particular attribute based on context, the `Sites` object comes in very handy.  For instance, the `.measureNumber` on a Note or other element, finds a container that is a Measure and has a `.number` attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.measureNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can do the same sort of thing, by calling the `Sites` object directly with the `getAttrByName` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.sites.getAttrByName('number')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or we can just get a list of sites that match a certain class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<music21.stream.Voice 0x10d4c7860>]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.sites.getSitesByClass(stream.Voice)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or with a string:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<music21.stream.Measure 1 offset=0.0>]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.sites.getSitesByClass('Measure')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that what is returned is a list, because an element can appear in multiple sites, even multiple sites of the same class, so long as those sites don't belong to the same hierarchy (that is, those streams are not both in the same stream or have a common stream somewhere in their Sites).  Let's put the note in another `Measure` object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "m10 = stream.Measure(number=10)\n",
    "m10.insert(20, es)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<music21.stream.Measure 1 offset=0.0>, <music21.stream.Measure 10 offset=0.0>]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.sites.getSitesByClass('Measure')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Users can iterate through all sites in a Stream using `.yieldSites()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "<music21.stream.Measure 1 offset=0.0>\n",
      "<music21.stream.Voice 0x10d4c7860>\n",
      "<music21.stream.Measure 10 offset=0.0>\n"
     ]
    }
   ],
   "source": [
    "for site in es.sites.yieldSites():\n",
    "    print(site)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note two things: (1) each element has a special site called \"None\" that stores information about the element when it is not in any Stream (it used to be used much more, but is not used as much anymore, and is not counted in the number of sites an element is in), and (2) the sites are yielded from earliest added to latest.  We can reverse it and eliminate None, with a few parameters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<music21.stream.Measure 10 offset=0.0>\n",
      "<music21.stream.Voice 0x10d4c7860>\n",
      "<music21.stream.Measure 1 offset=0.0>\n"
     ]
    }
   ],
   "source": [
    "for site in es.sites.yieldSites(excludeNone=True, sortByCreationTime=True):\n",
    "    print(site)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So effectively a `Note` or other `Music21Object` can always get its position in the streams that it is in via `.sites`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0 <music21.stream.Measure 1 offset=0.0>\n",
      "0.0 <music21.stream.Voice 0x10d4c7860>\n",
      "20.0 <music21.stream.Measure 10 offset=0.0>\n"
     ]
    }
   ],
   "source": [
    "for site in es.sites.yieldSites(excludeNone=True):\n",
    "    print(site.elementOffset(es), site)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Sites` object keeps track of the order of insertion through an attribute called `.siteDict`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "tags": [
     "nbval-ignore-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([(None, <music21.sites.SiteRef Global None Index>),\n",
       "             (4504830192,\n",
       "              <music21.sites.SiteRef 0/0 to <music21.stream.Measure 1 offset=0.0>>),\n",
       "             (4518082656,\n",
       "              <music21.sites.SiteRef 1/1 to <music21.stream.Voice 0x10d4c7860>>),\n",
       "             (4518134560,\n",
       "              <music21.sites.SiteRef 2/2 to <music21.stream.Measure 10 offset=0.0>>)])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es.sites.siteDict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each element has as its index the memory location of the site and a lightweight wrapper object around the site (i.e., stream) called a :class:`~music21.sites.SiteRef`.\n",
    "\n",
    "*(all sites except \"None\" are currently \"Stream\" objects -- is was our intention at the beginning to have other types of site contexts, such as interpretative contexts (\"baroque\", \"meantone tuning\") and we might still someday add those, but for now, a site is a Stream)*\n",
    "\n",
    "Let's look at the last one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.sites.SiteRef 2/2 to <music21.stream.Measure 10 offset=0.0>>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lastSiteRef = list(es.sites.siteDict.items())[-1][1]\n",
    "lastSiteRef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lastSiteRef.siteIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lastSiteRef.siteIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lastSiteRef.globalSiteIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This item allows `music21` to find sites by class without needing to unwrap the site"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Measure'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lastSiteRef.classString"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.stream.Measure 10 offset=0.0>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lastSiteRef.site"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## From weakness I get strength of memory\n",
    "\n",
    "From what has been shown so far, it appears that effectively the relationship between a stream and its containing element is a mirror or two-way: streams know what notes they contain and notes know what streams they are contained in.  But it is a bit more complicated and it comes from the type of reference each holds to each other.\n",
    "\n",
    "Streams hold elements with a standard or \"strong\" reference.  As long as the Stream exists in the computer's memory, all notes contained in it will also continue to exist.  You never need to worry about streams losing notes.\n",
    "\n",
    "The `.sites` object, does not, however, contain strong references to Streams.  Instead it contains what are called \"weak references\" to streams.  A weak reference allows notes and their `Sites` object to get access to the stream, **as long as it is still in use somewhere else**, but once the stream is no longer in use it is allowed to disappear anytime.\n",
    "\n",
    "As a demonstration, let's delete that pesky voice:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "del v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now whenever Python determines that it needs some extra space, the Note object will no longer have reference to the voice in its sites.  Note that, this might not happen immediately.  The removal of dead weak references, part of Python's garbage collection, takes place at odd times, dependent on the amount of memory currently used and many other factors.  So one cannot predict whether the `Voice` object would still be in the note's sites or not by the time you finish reading this paragraph.  `Music21` uses weak references in a number of other situations, such as \n",
    ":ref:`derivation objects <usersGuide_17_derivations>`, though we are reducing the number of places where they are used as the version of Python supported by `music21` gets a smarter and smarter garbage collector.\n",
    "\n",
    "You might be thinking that it's been years since the last time you called `del` on a variable, so this doesn't really apply to you.  But look at this code, which represents pretty typical music21 usage:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<music21.note.Note D>, <music21.note.Note C>)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = stream.Part()\n",
    "m = stream.Measure()\n",
    "p.insert(0, m)\n",
    "m.insert(1, note.Note('D'))\n",
    "firstNote = p.flat.notes[0]\n",
    "m.insert(0, note.Note('C'))\n",
    "newFirstNote = p.flat.notes[0]\n",
    "(firstNote, newFirstNote)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yup, there is a D in the part\n"
     ]
    }
   ],
   "source": [
    "for n in p.recurse():\n",
    "    if n is firstNote:\n",
    "        print('Yup, there is a D in the part')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Did you see where we created an extra stream only to immediately discard it?  The expression `p.flat` creates a new stream that exists just for long enough to get the first note from it.  (We actually store it in a cache on `p` so that it's faster the next time we need it, but once we add another note to `m` the cache is invalidated).  The creation of another stream is one reason to generally prefer `.recurse()` over `.flat`.  Prior to `music21` v.3, the `.notes` call would have created yet another Stream, but we've optimized this out."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "The reason why `music21` uses weak references instead of normal (strong) references to sites is to save some memory because how frequently objects, such as notes and streams, are copied.  When you run certain analytical or manipulation routines such as `toSoundingPitch()` or `stripTies()` or even common operations such as `.flat` and `show()`, copies of streams need to be made, often only to be discarded in a single line of code.  If every one of those streams, with all of their contents, persisted forever just because a single note from that stream stayed in memory, then the memory usage of `music21` would be much higher.\n",
    "\n",
    "Also note that the `Sites` object cleans up \"dead\" sites from time to time, and certain context-dependent calls, such as `.next(note.Rest)` or `.getContextByClass('Measure')` need to search every living site.  Over time, these calls would get slower and slower if otherwise long-forgotten streams created as byproducts of `.show()` or `.flat` stuck around."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's an example adapted from actual code that caused a problem.  The user was trying to figure out the beat for each note that was not the continuation of a tie, and he wrote:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "bach = corpus.parse('bach/bwv66.6')\n",
    "allNotes = list(bach.stripTies().recurse().notes)\n",
    "firstNote = allNotes[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Problems quickly arose though when he tried to figure out the note's beat via `firstNote.beat` -- it said that it was on beat 1, even though this piece in 4/4 began with a one-beat pickup, and should be on beat 4.\n",
    "\n",
    "What happened? Again, it's a problem with confusions from disappearing streams and sites.  The `.stripTies()` method creates a new score hierarchy, where each note in the score hierarchy is a copy derived from the previous one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<music21.note.Note C#>, <music21.note.Note C#>)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "firstNoteOriginal = bach.recurse().notes[0]\n",
    "firstNoteStripped = bach.stripTies().recurse().notes[0]\n",
    "(firstNoteOriginal, firstNoteStripped)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "firstNoteOriginal is firstNoteStripped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Derivation of <music21.note.Note C#> from <music21.note.Note C#> via 'stripTies'>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "firstNoteStripped.derivation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "firstNoteStripped.derivation.origin is firstNoteOriginal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So the Note object obtained from `.stripTies()` is not to be found in the original `bach` score:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bach.containerInHierarchy(firstNoteStripped) is None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.stream.Measure 0 offset=0.0>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bach.containerInHierarchy(firstNoteOriginal)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "in fact, firstNoteStripped's entire containerHierarchy is generally empty if garbage collection has run.  Why? Because firstNoteStripped only directly belongs to the hierarchy created by the unnamed and unsaved stream created by `stripTies()`.  So how to solve this?  In code that needs access to the hierarchy, make sure that it is preserved by saving it to a variable.  Here we will break up the code calling `stripTies()` and save it as a variable, `st_bach`.  Not only does this solve our problems, but it makes the code more readable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bach = corpus.parse('bach/bwv66.6')\n",
    "st_bach = bach.stripTies()\n",
    "allNotes = list(st_bach.recurse().notes)\n",
    "firstNote = allNotes[0]\n",
    "firstNote.beat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Doing this also fixes another thing that looked like a bug, but is expected behavior -- that `getContextByClass('Measure')` was needing to follow the derivation chain to find a measure that `firstNote` was not in.  Here it works as expected:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "firstNote.getContextByClass('Measure').elementOffset(firstNote)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This was a pretty dense chapter, I know, and it barely scratches the surface of the complexities of the Context system, so we'll move to something lighter if even more distant, and look at Medieval and Renaissance extensions in `music21`."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
